/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {boolean}
 */
var isPalindrome = function(head) {
  if(!head.next || !head) return true;
  let newHead = null, tempHead = head, length = 0;
  while(tempHead) {
    length++;
    tempHead = tempHead.next;
  }
  let count = Math.floor(length / 2), flag = length % 2;
  tempHead = head;
  while(tempHead && count) {
    count--;
    let temp = newHead;
    newHead = tempHead;
    tempHead = tempHead.next;
    newHead.next = temp;
  }
  if(flag) tempHead = tempHead.next;
  while(tempHead && newHead) {
    if(tempHead.val != newHead.val) return false;
    tempHead = tempHead.next;
    newHead = newHead.next;
  }
  return true;
};
let head = [1,2,3,3,2,1];
console.log(isPalindrome(head));
head = [1,2];
console.log(isPalindrome(head));
